﻿using MCPlatform.MotionLib.Base;
using MCPlatform.MotionLib.Factory.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.MotionLib.Factory
{
    public class Factory
    {
        #region 懒汉单例模式
        //构造函数私有化
        private Factory()
        {

            LTFactoryConfig();
        }

        //静态字段 volatile防止执行顺序不对
        private volatile static Factory? Instance;

        //新建引用类型对象，用于线程锁
        private static object o = new object();

        public static Factory GetInstance(IFactory iFactory, ushort motionCount, ushort valveCount, ushort ioModulesCount)
        {
            //双重判断，节省monitor enter 资源
            if (Instance == null)
            {
                lock (o)
                {
                    if (Instance == null)
                    {
                        _motionCount = motionCount;
                        _valveCount = valveCount;
                        _ioModulesCount = ioModulesCount;
                        _iFactory = iFactory;
                        Instance = new Factory();
                    }
                }
            }

            return Instance;

        }
        #endregion

        public Dictionary<string, IImputation>? Imputations { get; private set; }
        public Dictionary<string, IMotion>? Motions { get; private set; }
        public Dictionary<string, IValve>? Valves { get; private set; }
        public Dictionary<string, IIO>? IOs { get; private set; }

        private static ushort _imputationCount;

        private static ushort _motionCount;
        private static ushort _valveCount;
        //private static ushort _MultiMotionCount;
        private static ushort _ioModulesCount;
        private static IFactory? _iFactory;

       

        public void LTFactoryConfig()
        {
           
            Motions = _iFactory?.GetMotions(_motionCount);

            Valves = _iFactory?.GetValves(_valveCount);       

            IOs = _iFactory?.GetIOs(_ioModulesCount);

            Imputations = _iFactory?.GetImputations(_imputationCount);

        }
    }
}
